package cn.zzuisa.news.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

/**
 * @ClassName: JsonUtil.java
 * @Description: json转化工具类
 * @author zzuisa
 * @version V1.0
 * @Date 2018年9月4日 下午3:19:29
 */
public class JsonUtil {
	public static String getStrResponse(HttpServletRequest request) throws IOException {
		InputStream inputStream;
		String strResponse = "";
		try {
			inputStream = request.getInputStream();
			String strMessage = "";
			BufferedReader reader;
			reader = new BufferedReader(new InputStreamReader(inputStream, "utf-8"));
			/** 使用BufferedReader 读取数据流 */
			while ((strMessage = reader.readLine()) != null) {
				strResponse += strMessage;
			}
			reader.close();
			inputStream.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		/**传递给response 响应头 */
		return strResponse;
	}

	public static PrintWriter writer(HttpServletResponse response) throws IOException {
		return response.getWriter();
	}

	/**实体类转 json对象 格式 */
	public static JsonObject setData(Object object) {
		return new JsonParser().parse(new Gson().toJson(object)).getAsJsonObject();
	}
}
